package com.bmicpd.gzh.task;

import com.bmicpd.gzh.service.RealTimePushMessageService;
import com.bmicpd.gzh.vo.ResultVo;
import com.google.common.collect.Lists;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

/**
 * @author BigZ
 * @date 19-5-24 下午7:58
 */
@Component
@EnableScheduling
public class DeleteTask {

    private final Logger logger = LoggerFactory.getLogger(DeleteTask.class);

    @Value("${ElasticSearch.HOST}")
    private String esHost;

    @Value("${ElasticSearch.PORT}")
    private int esPort;

    @Value("${ElastisSearch.INDEX}")
    private String esIndex;

    @Value("${ElasticSearch.TYPE}")
    private String esType;

    @Scheduled(cron = "0 0/1 * * * ?")
    private void deleteNoneRecordTasks(){
        logger.info("hahahha");
        List<Map<String, Object>> list = Lists.newArrayList();
        // 创建客户端
        try (TransportClient client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddresses(
                new InetSocketTransportAddress(InetAddress.getByName(esHost),esPort))){
            logger.info("Elasticsearch connect info: {}", client);

            assert client != null;
            SearchRequestBuilder searchRequestBuilder = client.prepareSearch(esIndex)
                    .setTypes(esType).setTimeout(TimeValue.timeValueMillis(500))
                    .setQuery(QueryBuilders.matchPhraseQuery("irContent", "None"));
            long totalHits = searchRequestBuilder.get().getHits().getTotalHits();
            logger.info("query total : {}", totalHits);

            // 每次只获取15条结果
            SearchResponse searchResponse = searchRequestBuilder
                    .setFrom(0).setSize((int)totalHits).execute().actionGet();
            SearchHit[] hits = searchResponse.getHits().hits();
            for(SearchHit hit : hits){
                list.add(hit.getSource());
                logger.info(hit.getId());
                client.prepareDelete(esIndex, esType, hit.getId()).get();

            }


            logger.info(" query list size : {}", list.size());
        } catch (UnknownHostException e) {
            logger.error("Host 未知异常 ： {}", e);
        }

    }
}
